<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean" %>
<fmt:setBundle basename="messages"/>

<script type="text/javascript" language="javascript">

	var validSubmit = true;

	function submitShowTransportCostElements(form){
		if (validSubmit){
			form.action = "showAddPurchaseOrder.do";
			form.submit();
		} else {
			alert('<fmt:message key="warning.message.common.processing"/>');
		}
	}

	function submitSaveNewPurchaseOrder(form, flag){
		
		if (validSubmit && validatePurchaseOrderForm()){
			document.getElementById('purchaseOrderFlag').value = flag;

			formatFormAmounts();
			
			form.action = "saveNewPurchaseOrder.do";
			validSubmit = false;
			
			form.submit();
		}
	}

	function submitCancel(form){
		if (validSubmit){
			validSubmit = false;
			form.action = "listPurchaseProgramQuery.do";

			form.submit();
		}
	}

	function formatFormAmounts(){
		var purchaseOrderCapacity;
		var purchaseOrderCost;
		var purchaseOrderCosts;

		purchaseOrderCapacity = document.getElementById('purchaseOrderCapacity').value;
		document.getElementById('purchaseOrderCapacity').value = purchaseOrderCapacity.replace(/,/g, "");
		
		purchaseOrderCosts = document.getElementsByName('purchaseOrderCosts');
		
		for (x = 0; x < purchaseOrderCosts.length; x++){
			purchaseOrderCost = purchaseOrderCosts[x].value;
			purchaseOrderCosts[x].value = purchaseOrderCost.replace(/,/g, "");				
		}		
	}
	
	function validatePurchaseOrderForm(){
		var validPurchaseOrderForm = true;
		var purchaseOrderCode;
		var purchaseOrderCodeNumber;
		var purchaseOrderCosts;
		var purchaseOrderCapacity;
		var purchaseOrderCost;
		
		purchaseOrderCode = document.getElementById('purchaseOrderCode').value;
		purchaseOrderCodeNumber = document.getElementById('purchaseOrderCodeNumber').value;
		purchaseOrderCapacity = document.getElementById('purchaseOrderCapacity').value;
		purchaseOrderCosts = document.getElementsByName('purchaseOrderCosts');
		purchaseOrderCode = trim(purchaseOrderCode);
		purchaseOrderCodeNumber = trim(purchaseOrderCodeNumber);
		purchaseOrderCapacity = purchaseOrderCapacity.replace(/,/g, "");
		
		if (purchaseOrderCode.length == 0){
			validPurchaseOrderForm = false;
			alert('<fmt:message key="warning.message.purchaseorder.required.code"/>');
		} else if( purchaseOrderCodeNumber.length == 0 ){
			validPurchaseOrderForm = false;
			alert('<fmt:message key="warning.message.purchaseorder.required.codeNumber"/>');
		} else if (isNaN(parseFloat(purchaseOrderCapacity)) || parseFloat(purchaseOrderCapacity) <= 0.0 || parseFloat(purchaseOrderCapacity) > 99999.99){
			validPurchaseOrderForm = false;
			alert('<fmt:message key="warning.message.purchaseorder.invalid.capacity"/>');
		} else {
			for (x = 0; x < purchaseOrderCosts.length; x++){
				purchaseOrderCost = purchaseOrderCosts[x].value;
				purchaseOrderCost = purchaseOrderCost.replace(/,/g, "");
				
				if (isNaN(parseFloat(purchaseOrderCost)) || parseFloat(purchaseOrderCost) <= 0.0 || parseFloat(purchaseOrderCost) > 99999.99){
					validPurchaseOrderForm = false;
					alert('<fmt:message key="warning.message.purchaseorder.invalid.pricePerTon"/>');
					break;
				}
			}
		}

		return validPurchaseOrderForm;
	}
</script>
<form name="NewPurchaseOrderForm" method="POST">
	<table width="100%" align="center" border="0">
		<tr>
			<td style="width: 100%" align="center">
				<input type="hidden" name="flag" id="purchaseOrderFlag"/>
				<table style="width: 340" align="center" border="0" cellspacing="1" bgcolor="#CCCCC0">
					<tr>
						<th colspan="2" align="center" class="titulocolumna">
							<fmt:message key="label.purchaseorder.addNewTitle"/>
						</th>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.purchaseorder.transport"/>
						</td>
						<td class="etiqueta_campo">
							<select name="transportId" id="purchaseOrderTransportId" onchange="submitShowTransportCostElements(NewPurchaseOrderForm);" class="campo_texto">
								<c:forEach items="${transports}" var="transport" varStatus="status">
									<c:choose>
										<c:when test="${(transportView != null) && (transportView.transportIdentifier == transport.transportIdentifier)}">
											<option value="<c:out value="${transport.transportIdentifier}"/>" selected="selected"><c:out value="${transport.transportShortName}"/></option>
										</c:when>
										<c:otherwise>
											<c:choose>
												<c:when test="${status.first}">
													<option value="<c:out value="${transport.transportIdentifier}"/>" selected="selected"><c:out value="${transport.transportShortName}"/></option>
												</c:when>
												<c:otherwise>
													<option value="<c:out value="${transport.transportIdentifier}"/>"><c:out value="${transport.transportShortName}"/></option>
												</c:otherwise>
											</c:choose>
										</c:otherwise>								
									</c:choose>
								</c:forEach>
							</select>
						</td>
					</tr>
			        <tr> 
			          <td width="50%" class="etiqueta"><bean:message key="label.program.purchase.corntype"/></td>
			          <td width="50%" class="etiqueta_campo">
			          	<select  id="cornTypeId" style="width:160px" class="campo_texto" name="cornTypeId">			          		
			          		<c:forEach items="${cornTypes}" var="cornType" varStatus="status">
								<c:choose>
				          			<c:when test="${(transportView != null) && (transportView.cornTypesView.cornTypeId == cornType.cornTypeId)}">
				          				<option value="<c:out value="${cornType.cornTypeId}"/>" selected="selected"><c:out value="${cornType.description}"/></option>
				          			</c:when>								    
							    	<c:otherwise>
							    		<option value="<c:out value="${cornType.cornTypeId}"/>"><c:out value="${cornType.description}"/></option>
							    	</c:otherwise>  
							    	</c:choose>			
			          		</c:forEach>		          		
			       		</select>          
			          </td>
			        </tr>					
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.purchaseorder.code"/>
						</td>
						<td class="etiqueta_campo">
							<input type="text" name="code" id="purchaseOrderCode" onkeydown="return noPaste(event, this);" size="3" maxlength="5" onfocus="this.select();" class="campo_texto" value="<c:out value="${transportView.transportNomenclature}"/>"/> / <input class="columnanumero" type="text" name="codeNumber" id="purchaseOrderCodeNumber" size="3" maxlength="5"onfocus="this.select();" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,2,false);" />
							<fmt:message key="label.purchaseorder.formatprefix"/>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.purchaseorder.capacity"/>
						</td>
						<td class="etiqueta_campo">
							<input type="text" name="capacity" id="purchaseOrderCapacity" value="<fmt:formatNumber pattern="###,###,##0.000" value="${transportView.transportCapacity}"/>" onkeydown="return noPaste(event, this);" size="15" maxlength="10" onfocus="this.select();" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,3,false);" class="campo_texto" style="text-align: right;"/>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.purchaseorder.plant"/>
						</td>
						<td class="etiqueta_campo">
							<select name="plantId" class="campo_texto">
								<option value="-1"><fmt:message key="label.program.purchase.selectanoption"/></option>
								<c:forEach items="${plants}" var="plant" varStatus="status">
									<option value="<c:out value="${plant.plantId}"/>"><c:out value="${plant.description}"/></option>
								</c:forEach>
							</select>
						</td>						
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.purchaseorder.purchaseDate"/>
						</td>
						<td class="etiqueta_campo">
							<select name="biWeekPeriodId" class="campo_texto">
								<c:forEach items="${purchaseDates}" var="purchaseDate" varStatus="status">
									<c:choose>
										<c:when test="${purchaseDate.defaultInit}">
											<option value="<c:out value="${purchaseDate.biweekPeriodId}"/>" selected="selected"><c:out value="${purchaseDate.description}"/></option>
										</c:when>
										<c:otherwise>
											<option value="<c:out value="${purchaseDate.biweekPeriodId}"/>"><c:out value="${purchaseDate.description}"/></option>
										</c:otherwise>
									</c:choose>
								</c:forEach>
							</select>
						</td>
					</tr>
				</table>				
			</td>
		</tr>
		<tr>
			<td height="10">
			</td>
		</tr>
		<tr>
			<td width="100%">
				<table style="width: 340" align="center" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
					<tr>
						<td class="titulocolumna" style="text-align: center; width: 50%">
							<fmt:message key="label.purchaseorder.cost"/>
						</td>
						<td class="titulocolumna" style="text-align: center; width: 50%">
							<fmt:message key="label.purchaseorder.pricePerTon"/>
						</td>
					</tr>
					<c:forEach items="${transportView.transportCostElements}" var="transportCostElement" varStatus="status">
						<tr class="<c:choose><c:when test="${status.index%2 == 0}"><c:out value="columnadatoPar" /></c:when><c:otherwise><c:out value="columnadatoNon" /></c:otherwise></c:choose>">
							<td style="text-align: center; vertical-align: top; width: 50%">
								<c:out value="${transportCostElement.chargeTypeShortName}"/>
							</td>
							<td style="text-align: center; vertical-align: top" width="50%">
								<input type="hidden" name="purchaseOrderChargeIds" value="<c:out value="${transportCostElement.chargeTypeId}"/>"/>
								<input type="text" name="purchaseOrderCosts" value="<fmt:formatNumber pattern="###,###,##0.0000" value="${transportCostElement.chargeTypeCost}"/>" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,4,false);" maxlength="10" class="campo_texto" style="text-align: right;"/>
							</td>
						</tr>
					</c:forEach>
				</table>
			</td>
		</tr>
		<tr>
			<td width="100%">
				<table align="center" width="340" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
					<tr>
						<td style="text-align: center; vertical-align: bottom;" class="columnadatoPar">
							<input type="button" value="<fmt:message key="label.transports.add.saveTransport"/>" onclick="submitSaveNewPurchaseOrder(NewPurchaseOrderForm, 0);" class="boton"/>
							<input type="button" value="<fmt:message key="label.transports.add.saveAndNewTransport"/>" onclick="submitSaveNewPurchaseOrder(NewPurchaseOrderForm, 1);" class="boton_large"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<input type="button" value="<fmt:message key="label.common.cancel"/>" onclick="submitCancel(NewPurchaseOrderForm);" class="boton"/>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</form>